<?php
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/user/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/tucao/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/mblog/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/album/inc.php");
error_reporting(E_ERROR);


//所有的参数
$g_cgival 	= array();
//存储中间数据 非输出数据 可以不用
$g_pro	 	= array();
//要显示的数据 输出数据
$g_show 	= array();

function check_cgi_pro()
{		
	global $g_cgival, $g_pro, $g_show, $config;
	
	cgi::both($g_cgival["p"], "p", "");
	$g_cgival["p"] = intval($g_cgival["p"]);
	
	if($g_cgival["p"] < 1) $g_cgival["p"]=1;
	
	cgi::both($g_cgival["uid"], "uid", "");
	$g_cgival["uid"] = intval($g_cgival["uid"]);
}


function get_data()
{
	global $g_cgival, $g_pro, $g_show, $config;
	
	$db_user_info = new db_user_info();
	$user_info = new user_info();
	$is_login = $user_info->is_logined();
	$uid = $g_cgival["uid"];
	if($is_login)
	{
		$user_detail_info = $user_info->get_cur_user_info();
		
		$current_uid = $user_detail_info["uid"];
		$user_nickname = $user_detail_info["user_nickname"];
		$g_show["current_user_info"] = $user_detail_info;
		$g_show["is_logined"] = true;
	}
	else
	{
		$g_show["is_logined"] = false;
		$url = "/user/login.php?url={$_SERVER["REQUEST_URI"]}";
		header("Location:$url");
		exit;
	}
	
	$pcount = 20;
	$type=1;
	$p = $g_cgival["p"];
	
	$db_user_message  = new db_user_message();
	$count = $db_user_message->get_count_by_uid_detail($current_uid,$uid);
	$list = $db_user_message->get_list_by_uid_detail($current_uid,$uid,$p,$pcount);
	$db_user_message->update_to_have_read($uid,$current_uid);
	
	$uid_array = array();
	foreach($list as $key => $value)
	{
		if($current_uid == $value["from_uid"])
		{
			$uid_array[] = $value["to_uid"]; 
		}
		else
		{
			$uid_array[] = $value["from_uid"]; 
		}
		$list[$key]["create_time"] = TimeFormatter::timeFormat(strtotime($list[$key]["create_time"]) );
	
	}
	
	$uid_array = array_unique($uid_array);
	$mblog_cache_v2 = new mblog_cache_v2();
	$user_array = $mblog_cache_v2->cache_api_get_user_info_by_uids($uid,"",1);
	$g_show["to_uid"] = $uid;
	$g_show["to_user_info"] = $user_array["data"]["result"][0];
	
	$g_show["count"] = $count;
	$g_show["list"] = $list;
	
	
	
	$uids = array();
	$at_type_ids = array();
	$max_id =0;
	foreach($list as $key => $value)
	{
		if($max_id < $value["id"])
		{
			$max_id =  $value["id"];
		}
		$uids[] = $value["from_uid"];
		$at_type_ids[$value["at_type"]][] = $value["from_id"];
	}
	
	$db_user_read = new db_user_read();
	$user_read_info = $db_user_read->get_user_read_item_by_uid($uid);
	
	$read_at_id = $user_read_info["read_at_id"];
	
	if($read_at_id < $max_id)
	{
		$db_user_read->update_user_read_by_uid($uid,$max_id);
	}
	
	
	
	// 分页
	
	$Pages = new Pages($g_show["count"],$g_cgival["p"],$pcount);
	$g_show["page_bar"] = $Pages->getLinks("?uid=$uid&p=",5);
	
	
	$g_show["page_title"] ="私信详细页";

	
	//类型1
/*	$mod_mblog = new mod_mblog();
	list($list1,$list2,$list3,$list4) = $mod_mblog->get_topic_list_order_by_like();
	$g_show["list1"] = $list1;
	$g_show["list2"] = $list2;
	$g_show["list3"] = $list3;
	$g_show["list4"] = $list4;*/
	

	
}

try {
	$g_show['error'] = 0;
	$g_show['errmsg'] = "";
	check_cgi_pro();
	get_data();
	$g_show["hide_shadow"] = true;
} catch (Exception $e) {
	$g_show['error'] = 1;
	$g_show['errmsg'] = $e->getMessage();
}

show_pro();


exit;

function show_pro()
{
	global $g_cgival, $g_pro, $g_show, $config;

	$smarty = p_get_smarty();	
	$smarty->assign("g_show", $g_show);
	if (isset($_GET["debug"]))
	{
		print_r($g_show);
	}
	$smarty->display("user/message/user_message_detail.html");

	
	unset($g_cgival);
	unset($g_pro);
	unset($g_show);
	unset($config);
	
}

